﻿using Fishing.Abstractions;
using Fishing.Common;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Windows;

namespace Fishing.View
{
    public partial class FishingView : Window
    {
        private readonly IDesktopCore _desktopCore;
        private readonly IKeyBoardHookService _hookService;
        private readonly IThreadRipper _threadRipper;

        public FishingView()
        {
            InitializeComponent();

            WindowInitialized();

            _desktopCore = App.Services.GetRequiredService<IDesktopCore>();
            _hookService = App.Services.GetRequiredService<IKeyBoardHookService>();
            _threadRipper = App.Services.GetRequiredService<IThreadRipper>();

            Updater.GetViewModel().UpdateCompleted += FishingView_UpdateCompleted;

            Closing += FishingView_Closing;

            _desktopCore.FakeWallpaper();
        }

        private void FishingView_ForceQuitTriggered(object? sender, EventArgs e)
        {
            Application.Current.Dispatcher.Invoke(Close);
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            Updater.GetViewModel().Begin();
        }

        private void Window_SourceInitialized(object sender, EventArgs e)
        {
#if DEBUG
            //return;
#endif
            _hookService.EnableAllHook();
            _threadRipper.ThreadRip();
        }

        private void FishingView_Closing(object? sender, System.ComponentModel.CancelEventArgs e)
        {
            Restore();
        }

        private void FishingView_UpdateCompleted(object? sender, EventArgs e)
        {
            Application.Current.Dispatcher.Invoke(Close);
        }

        private void Restore()
        {
            Application.Current.Dispatcher.Invoke(() =>
            {
                _desktopCore.Restore();
                App.Services.GetRequiredService<MainWindow>().BtnFishing.IsEnabled = true;

                _hookService.EnableOnlyMainHook();
                _threadRipper.Restore();
            });
        }

        private void WindowInitialized()
        {
#if DEBUG
            WindowState = WindowState.Normal;
            WindowStyle = WindowStyle.SingleBorderWindow;
            Topmost = false;

            return;
#endif
            WindowState = WindowState.Maximized;
            WindowStyle = WindowStyle.None;
            Topmost = true;
        }

        private Command _forceQuitCommand;
        public Command ForceQuitCommand
        {
            get
            {
                if (_forceQuitCommand is null)
                {
                    _forceQuitCommand = new Command(
                        parms => ForceQuit());
                }

                return _forceQuitCommand;
            }
        }

        private void ForceQuit()
        {
            Updater.GetViewModel().Stop();
            Application.Current.Dispatcher.Invoke(Close);
        }
    }
}
